import uniIcon from '@/components/uni-icon/uni-icon.vue';
import uniCalendar from '@/components/uni-calendar/uni-calendar.vue';
import addOrderList from '@/components/selectGoodsItem/selectGoodsItem'
import selectSimple from '@/components/selectGoodsItem/selectSimple'
import * as Api from '@/syApis/syAddOrder/syaddApis.js'
import allConst from "@/Json.js"
import * as rules from "@/common/barcodeRules.js"
import jiuaiCheckbox from "@/components/jiuai-checkbox.vue";
import settingOption from "@/components/settingOption/settingOption.vue";
import * as ApiGet from '@/syApis/sySet/sySet.js'
export default {
	data() {
		return {
			CustomBar: this.CustomBar,
			//特价开始时间
			startTime: "",
			//特价结束时间
			endTime: "",
			isShowGoodsType: 0,
			typeName: "全部分类",
			date: '',
			timeData: {
				clockinfo: '',
				date: '',
				fulldate: '',
				lunar: '',
				month: '',
				range: '',
				year: ''
			},
			selectTab: 0,
			munelist: [{
				title: '单据信息',
			}, {
				title: '商品明细'
			}],
			TabCur: 0,
			scrollLeft: 0,
			list: [{
				index: 0,
				name: "单据信息"
			}],
			modalName: '',
			unitslist: [{
				unit: "件"
			}, {
				unit: "箱"
			}],
			unitselected: '',
			BillNo: "",
			//单据类型
			BillType: 1,
			IsAdd: 0,
			BillDate: "",
			//订单商品
			OrderGoodsList: [],
			//当前选择的商品
			NowSelectGoods: {},
			//商品数量
			GoodsQty: "",
			//商品折扣
			GoodsDiscount: "",
			//商品备注
			GoodsRemark: "",
			//商品库存
			GoodsStorm: "",
			//仓库的列表
			StoreList: [],
			//业务员的列表
			OperatorList: [],
			//供应商的列表
			ClientList: [],
			//制单人
			CreateName: "",
			//税率
			Tax: "",
			//商品列表查看更多标识
			selectLookMore: -1,
			//扫码多商品列表
			scanGoodList: [{
				GoodsName: "标题"
			}],
			//当前多商品选择的索引
			current: -1,
			//多规格商品弹框
			isShowScanModel: false,
			//选择了商品
			isSelectGood: false,
			//是否引用源单
			isYy: false,
			//搜索条件
			searchKey: "",
			//单据类型
			BillName: "采购订单",
			//单据类型编号
			// BillType: 1,
			ClientTitle: "供应商",
			//备注
			Remark: "",
			//修改标志
			isChange: false,
			//单据ID
			BillID: "",
			//修改的源单
			oldOrder: {},
			//供应商或者客户名称
			ClientName: "",
			//仓库名称搜索
			SearchStoreName: "",
			//当前选择的仓库条目
			SelectStoreObj: {
				StoreName: "",
				StoreID: ""
			},
			//当前选择的业务员条目
			SelectOptionsObj: {
				OperatorName: "",
				OperatorID: ""
			},
			//当前选择的供应商或者客户条目
			SelectClientObj: {
				ClientName: "",
				CreateID: ""
			},
			//业务员搜索
			SearchOptionName: "",
			//商品总数
			nowGoodCount: 0,
			//表头信息查看更多
			isLookHeaderMore: false,
			//查看更多提示
			TbHeaderMore: "查看更多",
			//扫码选择商品
			ScanGoodsModal: null,
			//返回顶部
			quiteTip: "返回\n顶部",
			//是否展示返回顶部
			isShowTip: false,
			//仓库临时数组
			TempStoreList: [],
			//业务员临时数组
			TempOptionList: [],
			//单据权限
			isUserRoot: {},
			unLoadimg: '../../static/unUplad.png',
			allConstUrl: allConst.imgurlHeader,
			//采购销售促销订单设置
			// cgSetting: {
			// 	BillType: 1,
			// 	// 远程打印
			// 	ycDy: [{
			// 		title: "保存后询问",
			// 		state: false
			// 	}, {
			// 		title: "直接打印",
			// 		state: false
			// 	}],
			// 	// 单据审核
			// 	djSh: [{
			// 		title: "保存后询问",
			// 		state: false
			// 	}, {
			// 		title: "直接审核",
			// 		state: false
			// 	}],
			// 	// 重复扫描时只累计数量
			// 	onlyAddNum: [{
			// 		title: "重复扫描时只累计数量",
			// 		state: false
			// 	}],
			// 	// 条码规则
			// 	BarcodeRule: [{
			// 		title: "条码规则",
			// 		state: false
			// 	}],
			// 	// 条码规则
			// 	isMoreSelectGood: [{
			// 		title: "是否多选商品",
			// 		state: false
			// 	}],
			// },
			//本地保存的设置数组
			cgSettingLocation: {},
			// 打印设置弹出框
			modalNames: null,
			//打印份数
			printNum: 1,
			//打印机编号
			printCode: 0,
			//默认系统设置
			settingList: [],
			isChangeShow: false,
			barCodeRelus: {
				Rule_Separate: "",
				Rule_Bar: "",
				Rule_Code: "",
				Rule_Num: "",
				Rule_Num_Demical: "",
				Rule_Price: "",
				Rule_Price_Demical: "",
				Rule_SN: "",
				Rule_BatchNo: "",
				Rule_Color: "",
				Rule_Size: "",
				Separate_IsEnable: "",
				BarRule_All_Length: "",
				BarRule_CutBar_Start: "",
				BarRule_CutBar_End: "",
				BarRule_CutCode_Start: "",
				BarRule_CutCode_End: "",
				BarRule_CutNum_Start: "",
				BarRule_CutNum_End: "",
				BarRule_CutNum_Demical: "",
				BarRule_CutPrice_Start: "",
				BarRule_CutPrice_End: "",
				BarRule_CutPrice_Demical: "",
				BarRule_CutSN_Start: "",
				BarRule_CutSN_End: "",
				BarRule_CutBatchNo_Start: "",
				BarRule_CutBatchNo_End: "",
				BarRule_CutColor_Start: "",
				BarRule_CutColor_End: "",
				BarRule_CutSize_Start: "",
				BarRule_CutSize_End: "",
				Cut_IsEnable: "",
				Ignore_Befor: "",
				Ignore_After: ""
			},
			//条码分割数组
			barRulesList: [],
			//是否启用分隔规则0 分割规则1 2不启用
			SeparateIsEnable: 0,
			isBillCheckQx: false,
			isGetPriceFollowMessage: [],
			AddGoodsTempList: [],
			isScanGoodMessage: [],
			tempScanGoodList: [],
			MasterPrintFormatList: [],
			SlavePrintFormatList: [],
			TableFootPrintFormatList: [],
			isSearch: false,
			isHide: false,

		}
	},
	components: {
		uniCalendar,
		uniIcon,
		addOrderList,
		jiuaiCheckbox,
		settingOption,
		selectSimple
	},
	onShow() {
		this.$eventHub.$off('clientListen');
		this.$eventHub.$off('fires');
		this.$eventHub.$off('fireData');
		this.$eventHub.$off('AddChangeBill');
		this.isHide = false;
		this.isSearch = false;
	},
	onPageScroll(e) {
		if (e.scrollTop > 400) {
			this.isShowTip = true;
		} else {
			this.isShowTip = false;
		}
	},
	onUnload() {
		console.log("页面卸载");
		try {
			Api.unRegisterReciver();
		} catch (e) {
			//TODO handle the exception
		}

	},
	watch: {
		//判断添加获取价格方案是否全部完成
		isGetPriceFollowMessage(newVal) {
			var that = this;
			console.log(newVal.length, this.AddGoodsTempList.length);
			if (newVal.length == this.AddGoodsTempList.length && newVal.length != 0) {
				setTimeout(function() {
					uni.hideLoading();
					that.isSeach = false;
					if (that.AddGoodsTempList.length > 0) {
						that.isInOrderList(that.AddGoodsTempList);
					}
				}, 200);
			}
		}
		// ,
		//判断扫码数据获取价格方案是否全部完成
		// isScanGoodMessage(newVal) {
		// 	var that = this;
		// 	if (newVal.length == this.tempScanGoodList.length && newVal.length != 0) {
		// 		setTimeout(function() {
		// 			uni.hideLoading();
		// 			let data = {
		// 				data: {
		// 					ResultMsg: that.tempScanGoodList
		// 				}
		// 			}
		// 			that.scanGoodList = that.pullBarCodeGoods(data).data.ResultMsg;
		// 			if (that.scanGoodList.length == 1) {
		// 				console.log("进来了");
		// 				that.selectGoodsList(0);
		// 			} else {
		// 				if (that.scanGoodList.length > 0) {
		// 					that.modalName = "StoreScanGoodModal";
		// 				}
		// 			}
		// 		}, 500);
		// 	}
		// }
	},
	onLoad(options) {
		var _this = this;
		this.BillName = options.BillName;
		this.BillType = options.BillName == '采购订单' ? 1 : options.BillName == "销售订单" ? 4 : 13;
		this.CreateName = uni.getStorageSync("UserName");
		this.ClientTitle = options.BillName == '采购订单' ? "供应商" : "客户";
		this.GetOperatorMessage();
		this.GetStoreMessage();
		uni.setNavigationBarTitle({
			title: options.BillName
		});
		this.getBillTime();
		//如果是改单
		console.log(options);
		if (options.type == 1) {
			let oldOrder = JSON.parse(options.oldOrder);
			this.oldOrder = oldOrder;
			this.BillNo = oldOrder.BillNo;
			this.timeData.fulldate = oldOrder.BillTime;
			this.CreateName = oldOrder.OpenName;
			this.SelectStoreObj.StoreName = oldOrder.stockName;
			this.SelectStoreObj.StoreID = oldOrder.StoreID;
			this.SelectOptionsObj.OperatorID = oldOrder.EmpID;
			this.SelectOptionsObj.OperatorName = oldOrder.EName;
			this.SelectClientObj.ClientName = oldOrder.BName;
			this.SelectClientObj.ClientID = oldOrder.ClientID;
			this.startTime = oldOrder.startTime.split(" ")[0];
			this.endTime = oldOrder.EndTime.split(" ")[0];
			this.getBillDetail(oldOrder.BillID);
			this.Remark = oldOrder.Comment;
			this.isChange = true;
		} else {
			this.isChange = false;
		}
		let PDA_Module_Android = uni.getStorageSync("PDA_Module_Android"); //获取单据权限
		PDA_Module_Android = JSON.parse(PDA_Module_Android);
		var that = this;
		PDA_Module_Android.forEach((item, index) => {
			if (this.BillName == item.ModuleName) {
				that.isUserRoot = item;
			}
		});
		let barCodeRelus = uni.getStorageSync("BarCode", "");
		if (barCodeRelus != "") {
			this.barCodeRelus = JSON.parse(barCodeRelus);;
		}
		let SystemSettingInfo = uni.getStorageSync("SystemSettingInfo", ""); //存储系统设置
		SystemSettingInfo = JSON.parse(SystemSettingInfo);
		SystemSettingInfo.forEach((item, index) => {
			if (item.SystemName == '单据是否直接审核') {
				if (item.Value == "1") {
					that.isBillCheckQx = true;
				}
			}
		});
		// let date = new Date();
		// var month = date.getMonth() + 1;
		// if (month < 10) {
		// 	month = "0" + month
		// }
		// var day = date.getDate();
		// if (day < 10) {
		// 	day = "0" + day;
		// }
		// this.BillDate = date.getFullYear() + "-" + month + "-" + day;
		// this.startTime = date.getFullYear() + "-" + month + "-" + day;
		// this.endTime = date.getFullYear() + "-" + month + "-" + day;

		try {
			Api.registerReciver(function(barcode) {

				_this.searchKey = barcode;
				_this.search(false);


			});
		} catch (e) {
			//TODO handle the exception
		}
		this.Select_MasterPrintFormat();
		this.Select_SlavePrintFormat();
		this.Select_TableFootPrintFormat();
	},
	computed: {
		goodAllAmount() {
			var Ammount = 0;
			var that = this;
			this.OrderGoodsList.forEach(item => {
				Ammount += that.compuTaxMoney(item);
			});
			return Math.round(Ammount * 100) / 100;
		},
		OrderGoodsListLength() {
			var AllCount = 0;
			var that = this;
			this.OrderGoodsList.forEach(item => {
				AllCount += parseFloat(item.Qty);
			});
			return AllCount;
		}
	},
	onHide() {
		this.isHide = true;
	},
	methods: {
		//获取打印头
		Select_MasterPrintFormat() {
			let data = {
				BillType: this.BillType,
				PrinterType: "ESC标准"
			}
			var that = this;
			ApiGet.Select_MasterPrintFormat(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg != "") {

					} else {
						for (var i = 0; i < res.data.ResultMsg.length - 1; i++) {
							for (var j = 0; j < res.data.ResultMsg.length - 1 - i; j++) {
								// 相邻元素两两对比，元素交换，大的元素交换到后面
								if (res.data.ResultMsg[j] > res.data.ResultMsg[j + 1]) {
									var temp = res.data.ResultMsg[j];
									res.data.ResultMsg[j] = res.data.ResultMsg[j + 1];
									res.data.ResultMsg[j + 1] = temp;
								}
							}
						}
						that.MasterPrintFormatList = res.data.ResultMsg;
					}
				} else {

				}
			});
		},
		//获取打印内容
		Select_SlavePrintFormat() {
			let data = {
				BillType: this.BillType,
				PrinterType: "ESC标准"
			}
			var that = this;
			ApiGet.Select_SlavePrintFormat(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg != "") {

					} else {
						for (var i = 0; i < res.data.ResultMsg.length - 1; i++) {
							for (var j = 0; j < res.data.ResultMsg.length - 1 - i; j++) {
								// 相邻元素两两对比，元素交换，大的元素交换到后面
								if (res.data.ResultMsg[j] > res.data.ResultMsg[j + 1]) {
									var temp = res.data.ResultMsg[j];
									res.data.ResultMsg[j] = res.data.ResultMsg[j + 1];
									res.data.ResultMsg[j + 1] = temp;
								}
							}
						}
						that.SlavePrintFormatList = res.data.ResultMsg;
					}
				} else {

				}
			});
		},
		//获取打印尾
		Select_TableFootPrintFormat() {
			let data = {
				BillType: this.BillType,
				PrinterType: "ESC标准"
			}
			var that = this;
			ApiGet.Select_TableFootPrintFormat(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg != "") {

					} else {
						for (var i = 0; i < res.data.ResultMsg.length - 1; i++) {
							for (var j = 0; j < res.data.ResultMsg.length - 1 - i; j++) {
								// 相邻元素两两对比，元素交换，大的元素交换到后面
								if (res.data.ResultMsg[j] > res.data.ResultMsg[j + 1]) {
									var temp = res.data.ResultMsg[j];
									res.data.ResultMsg[j] = res.data.ResultMsg[j + 1];
									res.data.ResultMsg[j + 1] = temp;
								}
							}
						}
						that.TableFootPrintFormatList = res.data.ResultMsg;
					}
				} else {

				}
			});
		},
		getBillTime() {
			let date = new Date();
			var month = date.getMonth() + 1;
			if (month < 10) {
				month = "0" + month
			}
			var day = date.getDate();
			if (day < 10) {
				day = "0" + day;
			}
			this.BillDate = date.getFullYear() + "-" + month + "-" + day;
			this.startTime = date.getFullYear() + "-" + month + "-" + day;
			this.endTime = date.getFullYear() + "-" + month + "-" + day;

		},
		//价格跟踪
		XH_Get_PriceFollowMessage: async function(priceItem, Filter = "", success, isScan = false) {
			var that = this;
			let data = {
				GoodsID: priceItem.GoodsID,
				UnitID: priceItem.UnitID,
				BillName: this.BillName,
				URate: priceItem.urate,
				ClientID: this.SelectClientObj.ClientID
			}
			Api.XH_Get_PriceFollowMessage(data, function(res) {
				// console.log(Filter);
				if (isScan) {
					that.isScanGoodMessage.push(Filter);
				} else {
					that.isGetPriceFollowMessage.push(Filter);
				}
				if (res.state) {
					if (res.data.ResultMsg != "") {
						// that.PriceList = res.data.ResultMsg;
						// priceItem.Price = that.PriceList[0].Price;
						success(res.data.ResultMsg[0].Price, Filter);

					} else if (res.data.ErrorMsg == '获取登录信息失败') {
						Api.showTip(res.data.ErrorMsg);
					} else {
						Api.showTip(res.data.ErrorMsg);
					}

				} else {

				}
			});
		},
		newMoreInput() {
			this.sureClear();
			let settingList = uni.getStorageSync("settingList", "");
			if (settingList != "") {
				settingList = JSON.parse(settingList);
				console.log(settingList);
				settingList.forEach((item, index) => {
					if (this.BillType != 13) {
						if (item.Default_Name == '供应商' && this.BillType != 4) {
							this.SelectClientObj.ClientName = item.Default_Value;
							this.SelectClientObj.ClientID = item.Default_ID;
						} else if (item.Default_Name == '客户' && this.BillType == 4) {
							this.SelectClientObj.ClientName = item.Default_Value;
							this.SelectClientObj.ClientID = item.Default_ID;
						}
					}

					if (item.Default_Name == "业务员") {
						this.SelectOptionsObj.OperatorName = item.Default_Value;
						this.SelectOptionsObj.OperatorID = item.Default_ID;
					}
					if (item.Default_Name == '仓库') {
						this.SelectStoreObj.StoreName = item.Default_Value;
						this.SelectStoreObj.StoreID = item.Default_ID;
					}
				});

			}
		},
		//切换商品列表
		changeSelectShow() {
			this.isChangeShow = !this.isChangeShow;
		},
		//打印机编号选择事件
		radioChange(e) {
			this.printCode = e.detail.value
		},
		getPrintNum(e) {
			this.printNum = e.detail.value
		},
		//远程打印
		ycDy() {
			if (this.isChange) {
				console.log(this.$refs.settingOption._data);
				this.$refs.settingOption._data.modalPrintName = "printSetting";
				// this.modalNames = 
			} else {
				Api.showTip("请先查询单据");
			}
		},
		//小票打印
		xpDy() {
			if (this.isChange) {
				// console.log(this.$refs.settingOption._data);
				// this.$refs.settingOption._data.modalPrintName = "printSetting";
				// this.modalNames = 
				console.log("小票打印ssssss");
				let item = {
					Save_M_Data_Json: this.oldOrder,
					Save_D_Data_Json: this.OrderGoodsList
				}
				console.log(item);
				let mac_address = uni.getStorageSync("bluetoothName", "");
				// if (mac_address == "") {
				// 	Api.showTip("请先连接打印机");
				// 	return;
				// }
				rules.print(mac_address, item, this.BillType, this.MasterPrintFormatList, this.SlavePrintFormatList, this.TableFootPrintFormatList);

			} else {
				Api.showTip("请先查询单据");
			}
		},
		// //单据审核
		djSh() {
			if (this.isChange) {
				this.isBillCheck();
			} else {
				Api.showTip("请先查询单据");
			}
		},
		changeSetting(e) {
			this.cgSettingLocation = e.detail;
			this.isChangeShow = this.cgSettingLocation.GoodShowSimpleInfo[0].state;
		},
		//关闭打印设置弹框
		cancelModel() {
			this.modalNames = null;
		},
		//保存当前的设置
		// settingNow() {
		// 	uni.setStorageSync("setting" + this.BillType, JSON.stringify(this.cgSettingLocation));
		// },
		//切换表格风格
		onNavigationBarButtonTap(e) {
			const index = e.index;
			console.log(index);
			if (index == 0) {
				this.modalName = 'DrawerModalR';
			}
		},
		DateChange(e) {
			this.BillDate = e.detail.value;
		},
		DateChangeStart(e) {
			this.startTime = e.detail.value;
		},
		DateChangeEnd(e) {
			this.endTime = e.detail.value;
		},
		filterStore(e) {
			let value = e.detail.value;
			this.TempStoreList = [];
			var that = this;
			this.StoreList.forEach((item, index) => {
				if (item.StoreName.indexOf(value) > -1) {
					that.TempStoreList.push(item);
				}
			});
			// console.log(that.TempStoreList);
			if (that.TempStoreList.length == 0) {
				Api.showTip("查无数据");
			}
		},
		filterOption(e) {
			let value = e.detail.value;
			this.TempOptionList = [];
			var that = this;
			this.OperatorList.forEach((item, index) => {
				if (item.OperatorName.indexOf(value) > -1) {
					that.TempOptionList.push(item);
				}
			});
			if (that.TempOptionList.length == 0) {
				Api.showTip("查无数据");
			}
		},
		//返回顶部
		scrollTop() {
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 0
			});
		},
		//新增扫码
		// selectScanGood(index) {
		// 	this.AddListens();
		// 	this.modalName = "";
		// 	this.searchKey = "";
		// 	let type = this.isChange ? "1" : "0";
		// 	uni.setStorageSync("ChangeGoodData", JSON.stringify(this.scanGoodList[index]));
		// 	uni.navigateTo({
		// 		url: "../goodList/addScanGood?selectIndex=" +
		// 			index + "&IsCx=" + this.BillType + "&type=" + type + "&BillName=" + this.BillName
		// 	});
		// 	// console.log(this.BillName)
		// },
		//查看单据
		lookOrder() {
			// this.AddChangeBill();
			if (this.isChange) {
				if (this.BillType == 13) {
					uni.redirectTo({
						url: '/pages/selPurchaseOrderCx/selPurchase?BillName=' + this.BillName
					})
				} else {
					uni.redirectTo({
						url: '/pages/selPurchaseOrder/selPurchase?BillName=' + this.BillName
					})
				}
			} else {
				if (this.BillType == 13) {
					uni.navigateTo({
						url: '/pages/selPurchaseOrderCx/selPurchase?BillName=' + this.BillName
					})
				} else {
					uni.navigateTo({
						url: '/pages/selPurchaseOrder/selPurchase?BillName=' + this.BillName
					})
				}
			}

		},
		//修改商品
		AddChangeBill() {
			var _this = this;
			//当前选择的商品的临时列表
			// var NewGoodListTemp = this.OrderGoodsList;
			this.$eventHub.$on('AddChangeBill', function(data) {
				console.log("12111");
				// let GoodsItem = JSON.parse(data);	
				// // console.log(_this.OrderGoodsList);
				// _this.pullGoodsInfo(GoodsItem);
				let oldOrder = JSON.parse(data);
				_this.oldOrder = oldOrder;
				_this.BillNo = oldOrder.BillNo;
				_this.timeData.fulldate = oldOrder.BillTime;
				_this.CreateName = oldOrder.CreateName;
				_this.SelectStoreObj.StoreName = oldOrder.stockName;
				_this.SelectStoreObj.StoreID = oldOrder.StoreID;
				_this.SelectOptionsObj.OperatorID = oldOrder.EmpID;
				_this.SelectOptionsObj.OperatorName = oldOrder.EName;
				_this.SelectClientObj.ClientName = oldOrder.BName;
				_this.SelectClientObj.ClientID = oldOrder.ClientID;
				_this.startTime = oldOrder.startTime.split(" ")[0];
				_this.endTime = oldOrder.EndTime.split(" ")[0];
				_this.Remark = oldOrder.Comment;
				_this.getBillDetail(oldOrder.BillID);
				_this.isChange = true;
				_this.$eventHub.$off('AddChangeBill');
			});
		},
		//清空单据
		clearAll() {
			var that = this;
			uni.showModal({
				title: "是否清空单据",
				content: "清空后不可恢复,是否确认",
				success(res) {
					if (res.confirm) {
						that.sureClear();
					}
				}
			})
		},
		sureClear(isBill=false) {
			this.BillNo = "";
			this.OrderGoodsList = [];
			let date = new Date();
			var month = date.getMonth() + 1;
			if (month < 10) {
				month = "0" + month
			}
			var day = date.getDate();
			if (day < 10) {
				day = "0" + day;
			}
			this.BillDate = date.getFullYear() + "-" + month + "-" + day;
			this.startTime = date.getFullYear() + "-" + month + "-" + day;
			this.endTime = date.getFullYear() + "-" + month + "-" + day;
			this.BillNo = "";
			this.SelectStoreObj = {
				StoreName: "",
				StoreID: ""
			};
			this.Remark = "";
			//当前选择的业务员条目
			this.SelectOptionsObj = {
					OperatorName: "",
					OperatorID: ""
				},
				//当前选择的供应商或者客户条目
				this.SelectClientObj = {
					ClientName: "",
					CreateID: ""
				}
				console.log(this.isChange);
				if(isBill){
					
						this.GetNewOrderNo(false);
					
				}
				this.isChange = false;
				
		},
		//查看表头更多信息
		lookHeaderMore() {
			this.isLookHeaderMore = !this.isLookHeaderMore;
			if (this.isLookHeaderMore) {
				this.TbHeaderMore = "收起更多"
			} else {
				this.TbHeaderMore = "查看更多"
			}
		},
		//删除选择的商品
		deleteSelectGood(e) {
			let index = e.currentTarget.detail.deleteIndex;
			var _this = this;
			Api.showConfirm("是否删除此商品", function(state) {
				if (state) {
					_this.removeItem(index);
				} else {

				}
			})
		},
		//选择头部
		selectTabs(index) {
			this.TabCur = index;
			this.selectTab = index;
		},
		//显示弹框
		showModal(e) {
			this.modalName = e.currentTarget.dataset.target
		},
		//选择仓库
		selectStore(index) {
			this.SelectStoreObj = this.StoreList[index];
			this.modalName = "";
		},
		hideModal() {
			this.modalName = "";
		},
		//选择业务员
		selectOptions(index) {
			this.SelectOptionsObj = this.OperatorList[index];
			this.modalName = "";
		},
		//选择供应商
		AddClientListen() {
			let isClient = this.BillType == 1 ? 1 : 0
			uni.navigateTo({
				url: '../clientSelect/clientSelect?isClient=' + isClient + ""
			})
			var that = this;
			this.$eventHub.$on('clientListen', function(data) {
				that.SelectClientObj = JSON.parse(data);
				that.$eventHub.$off('clientListen');
			})
		},
		//搜索业务员
		searchOption() {
			if (this.searchOption != '') {
				// let Filter = "OperatorName like '%" + this.SearchOptionName + "%'";
				let Filter = "OperatorName lile '%" + this.SearchOptionName + "%'";
				this.GetOperatorMessage(Filter);
			} else {
				this.GetOperatorMessage();
			}
		},
		//搜索仓库
		searchStore() {
			if (this.SearchStoreName != '') {
				let Filter = "StoreName like '%" + this.SearchStoreName + "%'";
				this.GetStoreMessage(Filter);
			} else {
				this.GetStoreMessage();
			}
		},
		//获取单据明细
		getBillDetail(BillNo) {
			this.BillID = BillNo;
			let data = {
				BillID: BillNo
			};
			var that = this;
			Api.SelectBillOrderDetail(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == '获取登录信息失败') {

					} else if (res.data.ErrorMsg != '') {
						Api.showTip(res.data.ErrorMsg);
					} else {
						res.data.ResultMsg.forEach((item, index) => {
							res.data.ResultMsg[index].DiscountPrice = that.DiscountPrice(item);
							res.data.ResultMsg[index].Amount = that.Amount(item);
							res.data.ResultMsg[index].stockqty = item.stock_qty;
							res.data.ResultMsg[index].IsGift = item.IsGift ? 1 : 0;
							res.data.ResultMsg[index].urate = item.URate;
							if (res.data.ResultMsg[index].Goods_Image_Path1 == '' || typeof res.data.ResultMsg[index].Goods_Image_Path1 !=
								'string') {
								res.data.ResultMsg[index].Goods_Image_Path1 = that.unLoadimg;
							} else {
								res.data.ResultMsg[index].Goods_Image_Path1 = allConst.pictureUrl + res.data.ResultMsg[index].Goods_Image_Path1;
							}
						})
						that.OrderGoodsList = res.data.ResultMsg;
					}
				}
			})
		},
		DiscountPrice(item) {
			return parseFloat(item.Price) * (parseFloat(item.Discount) / 100) * (1 + parseFloat(
				item.Tax) / 100);
		},
		//计算总价
		Amount(item) {
			return parseFloat(item.Price) * parseFloat(item.Qty) * (parseFloat(item.Discount) / 100) * (1 + parseFloat(
				item.Tax) / 100);
			// return  PFPrice;
		},
		choosesGoods(index) {
			this.NowSelectGoods = this.scanGoodList[index];
			console.log(this.NowSelectGoods);
			this.isShowScanModel = false;
			this.isSelectGood = true;
		},
		// radioChange(evt) {
		// 	this.NowSelectGoods = this.scanGoodList[evt.target.value];
		// 	console.log(this.NowSelectGoods);
		// 	this.isShowScanModel = false;
		// 	this.isSelectGood = true;
		// }, 
		search(flag = true) {
			this.isGetPriceFollowMessage = [];
			//如果不是扫描头
			//如果是开启了快速扫描
			if (this.cgSettingLocation.QuiteAddGoodInfo[0].state) {
				flag = false;
			} else {
				flag = true;
			}
			this.isScanGoodMessage = [];
			// this.isGetPriceFollowMessage = [];
			if (this.SelectStoreObj.StoreID == '') {
				Api.showTip("请先选择仓库");
				return;
			}
			if (this.SelectClientObj.ClientID == "" && this.BillType == 1) {
				uni.showToast({
					title: "请先选择供应商"
				});
				return;
			}
			if (this.SelectClientObj.ClientID == "" && this.BillType == 4) {
				uni.showToast({
					title: "请先选择客户"
				});
				return;
			}
			if (this.isSearch) {
				Api.showTip("数据处理中,请稍后");
				return;
			}

			if (this.isHide) {
				return;
			}
			this.searchKey  =this.searchKey.replace(/^\s+|\s+$/g,"");
			//是否启用条码规则
			let finalObj = rules.getRulesStr(this.cgSettingLocation.BarcodeRule[0].state,
				this.barCodeRelus, this.searchKey);
			console.log(finalObj);
			this.isGetPriceFollowMessage = [];
			//判断启用的条码规则
			this.SeparateIsEnable = finalObj.SeparateIsEnable;
			this.barRulesList = finalObj.barRulesList;
			// this.searchKey = ;
			this.GetBarcodeGoodsMessageModel(finalObj.searchKey, flag);
			// if (this.cgSettingLocation.BarcodeRule[0].state) {
			// 	if (this.barCodeRelus.Separate_IsEnable == 1) {
			// 		this.SeparateIsEnable = 0;
			// 		if (this.barCodeRelus.Rule_Separate != "") {
			// 			this.barRulesList = this.searchKey.split(this.barCodeRelus.Rule_Separate);
			// 			console.log(parseFloat(this.barCodeRelus.Rule_Bar));
			// 			if (parseInt(this.barCodeRelus.Rule_Bar) <= this.barRulesList.length) {
			// 				// console.log(this.barRulesList[parseInt(this.barCodeRelus.Rule_Bar)]);
			// 				this.GetBarcodeGoodsMessageModel(this.barRulesList[parseInt(this.barCodeRelus.Rule_Bar) - 1]);
			// 			} else {
			// 				this.GetBarcodeGoodsMessageModel(this.searchKey);
			// 			}
			// 		}
			// 	}
			// 	// 截取规则
			// 	else if (this.barCodeRelus.Cut_IsEnable == 1) {
			// 		this.SeparateIsEnable = 1;
			// 		console.log(this.barCodeRelus.BarRule_All_Length, this.searchKey.length);
			// 		if (this.barCodeRelus.BarRule_All_Length == this.searchKey.length && this.searchKey != "") {
			// 			if (this.barCodeRelus.barCodeRelus != "" && this.barCodeRelus.BarRule_CutBar_End != "") {
			// 				// barCodeStart = this.barCodeRelus.BarRule_CutBar_Start;
			// 				this.GetBarcodeGoodsMessageModel(this.searchKey.substring(this.barCodeRelus.BarRule_CutBar_Start, this.barCodeRelus
			// 					.BarRule_CutBar_End));
			// 			} else {
			// 				this.GetBarcodeGoodsMessageModel(this.searchKey);
			// 			}
			// 			// if(this.barCodeRelus.BarRule_CutBar_End!=""){
			// 			// 	barCodeEnd = this.barCodeRelus.BarRule_CutBar_End;
			// 			// }

			// 		} else {
			// 			this.GetBarcodeGoodsMessageModel(this.searchKey);
			// 		}

			// 	} else {
			// 		this.SeparateIsEnable = 2;
			// 		this.GetBarcodeGoodsMessageModel(this.searchKey);
			// 	}
			// }
		},
		//扫码获取商品
		tel: function() {
			// Api.showTip("网页端暂不支持此功能");
			// return;
			this.current = -1;
			// 调起条码扫描
			var _this = this;

			uni.scanCode({
				scanType: 'barCode',
				success: function(res) {
					_this.isGetPriceFollowMessage = [];
					// this.isGetPriceFollowMessage = [];
					//如果不是扫描头
					//如果是开启了快速扫描
					var flag = true;
					if (_this.cgSettingLocation.QuiteAddGoodInfo[0].state) {
						flag = false;
					} else {
						flag = true;
					}
					_this.GetBarcodeGoodsMessageModel(res.result, flag);
					console.log('条码类型：' + res.scanType);
					console.log('条码内容：' + res.result);
				},
				fail(e) {
					console.log(e);
				}
			});
		},
		//修改还是新增单据
		isChangeOrder() {
			//如果新增
			if (this.isChange) {
				this.Change_Bill_Order();
			} else {
				this.GetNewOrderNo(true);
			}
		},
		//修改单据
		Change_Bill_Order() {
			this.searchKey = "";
			//如果是促销单
			if (this.BillType == 13) {
				if (this.startTime == "") {
					Api.showTip("请选择特价开始时间");
					return;
				}
				if (this.endTime == "") {
					Api.showTip("请选择特价结束时间");
					return;
				}
			}
			if (this.SelectClientObj.ClientID == "") {
				Api.showTip("请先选择" + this.ClientTitle);
				return;
			}
			if (this.SelectStoreObj.StoreID == "") {
				Api.showTip("请先选择仓库");
				return;
			}
			let Total = 0;
			this.OrderGoodsList.forEach((item, index) => {
				Total += this.compuTaxMoney(item);
			});
			let Save_M_Data_Json = {
				ClientID: this.SelectClientObj.ClientID,
				StoreID: this.SelectStoreObj.StoreID,
				EmpID: this.SelectOptionsObj.OperatorID,
				Amount: Total + "",
				Comment: this.Remark,
				startTime: this.startTime,
				EndTime: this.endTime
			}
			var Save_D_Data_Json = [];
			//console.tablele(this.OrderGoodsList);
			this.OrderGoodsList.forEach((item, index) => {
				console.log(item.StoreID);
				let itemData = {
					BillType: this.BillType + "",
					GoodsID: item.GoodsID,
					UnitID: item.UnitID,
					URate: item.urate + "",
					Discount: item.Discount + "",
					Tax: item.Tax + "",
					Qty: item.Qty + "",
					Price: item.Price + "",
					Amount: this.compuTaxMoney(item) + "",
					Remark: item.Remark,
					String3: item.barcode,
					StoreID: item.StoreID == "underfind" ? _this.SelectStoreObj.StoreID : item.StoreID
				};
				Save_D_Data_Json.push(itemData);
			});
			let data = {
				BillID: this.BillID,
				Save_M_Data_Json: JSON.stringify(Save_M_Data_Json),
				Save_D_Data_Json: JSON.stringify(Save_D_Data_Json)
			};
			var that = this;
			Api.Change_Bill_Order(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == "获取登录信息失败") {
						Api.showTip("登录超时");
					} else if (res.data.ErrorMsg != "") {
						Api.showTip(res.data.ErrorMsg);
					} else {
						that.sureClear(true);
						Api.showTip("提交成功");
					}
				} else {

				}
			});
		},
		//通过条码扫描商品
		GetBarcodeGoodsMessageModel(barcode, flag = true) {
			this.isSearch = true;
			uni.showLoading({
				title: "加载中",
				mask: false
			})
			let Model = "";
			if (this.cgSettingLocation.BarLike[0].state) {
				Model = "%like"
			} else if (this.cgSettingLocation.BarLike[1].state) {
				Model = "like%"
			} else if (this.cgSettingLocation.BarLike[2].state) {
				Model = "like"
			} else {
				Model = "="
			}
			let data = {
				BillName: this.BillName,
				BarCode: barcode,
				Model: Model,
				StoreID: this.SelectStoreObj.StoreID
			};
			var that = this;
			Api.GetBarcodeGoodsMessageModel(data, function(res) {
				that.searchKey = "";
				console.log(res.data.ResultMsg);
				// Api.showTip(res.data.ResultMsg);
				if (res.data.ResultMsg != '') {
					if (res.data.ResultMsg.length == 0) {
						Api.showTip("查无数据");
						that.isSearch = false;
						return;
					}
					res.data.ResultMsg.forEach((item, index) => {
						if (res.data.ResultMsg[index].Goods_Image_Path1 == "") {
							res.data.ResultMsg[index].Goods_Image_Path1 = that.unLoadimg
							// console.log("进来了");
						} else {
							console.log("----进了了");
							res.data.ResultMsg[index].Goods_Image_Path1 = that.allConstUrl + res.data.ResultMsg[index].Goods_Image_Path1;
						}
					})
					if (flag) {
						uni.hideLoading();
						that.scanGoodList = that.pullBarCodeGoods(res).data.ResultMsg;
						if (that.scanGoodList.length == 1) {
							console.log("进来了");
							that.selectGoodsList(0);
						} else {
							if (that.scanGoodList.length > 0) {
								that.modalName = "StoreScanGoodModal";
								that.isSearch = false;
							}
						}
					} else {
						res = that.pullBarCodeGoods(res).data.ResultMsg;
						let resultList = [];
						resultList.push(res[0]);
						console.log(resultList);
						that.pullGoodsInfo(resultList);
					}
				} else if (res.data.ResultMsg == "") {
					uni.hideLoading();
					Api.showTip("查无数据");
					that.isSearch = false;
				} else if (res.data.ErrorMsg == "获取登录信息失败") {
					uni.hideLoading();
					that.isSearch = false;
				} else {
					uni.hideLoading();
					that.isSearch = false;
				}
			})
		},
		//赋值条码规则的商品
		pullBarCodeGoods(res) {
			var that = this;
			// console.log(this.barCodeRelus);
			res.data.ResultMsg.forEach((item, index) => {
				res.data.ResultMsg[index].Qty = 1;
				let result = rules.ruleFinalRes(that.SeparateIsEnable, that.barCodeRelus, that.barRulesList, res, that.searchKey,
					index);
				res = result.result;
				if (!result.isChange) {
					res.data.ResultMsg[index].Price = res.data.ResultMsg[index].Price;
				}
				res.data.ResultMsg[index].Discount = 100;
				res.data.ResultMsg[index].Tax = 0;
				res.data.ResultMsg[index].Remark = "";
				res.data.ResultMsg[index].GoodsStorm = 0;
				res.data.ResultMsg[index].barcode = res.data.ResultMsg[index].BarCode;
				res.data.ResultMsg[index].URate = res.data.ResultMsg[index].urate;
				res.data.ResultMsg[index].StoreID = that.SelectStoreObj.StoreID;
				res.data.ResultMsg[index].stockqty = Math.round(res.data.ResultMsg[index].stockqty * 100) / 100;
				res.data.ResultMsg[index].IsGift = 0;
				res.data.ResultMsg[index].DiscountPrice = that.DiscountPrice(res.data.ResultMsg[index]);
				res.data.ResultMsg[index].Amount = that.Amount(res.data.ResultMsg[index]);
			});
			return res;
		},
		selectGoodsList(index) {
			this.AddListen();
			this.isSearch = true;
			this.searchKey = "";
			this.modalName = "";
			uni.setStorageSync("ChangeGoodData", JSON.stringify(this.scanGoodList[index]));
			uni.navigateTo({
				url: "../goodList/addScanGood?index=" + index + "&StoreID=" + this.SelectStoreObj.StoreID + "&IsCx=" + this.BillType +
					"&BillName=" + this.BillName + "&ClientID=" + this.SelectClientObj.ClientID
			});
		},
		//移除
		removeItem($index) {
			//删除操作存储未被删除的数据
			var newGoodList = [];
			this.OrderGoodsList.forEach((item, index) => {
				if (index != $index) {
					newGoodList.push(item);
				}
			});
			this.OrderGoodsList = newGoodList;
		},
		//获取更多商品信息
		lookGoodsInfo(index) {
			if (this.selectLookMore == index) {
				this.selectLookMore = -1;
			} else {
				this.selectLookMore = index;
			}
		},
		//修改商品信息
		changeInfo(e) {
			if (this.SelectClientObj.ClientID == "" && this.BillType == 4) {
				Api.showTip("请选择客户");
				return;
			}
			if (this.SelectClientObj.ClientID == '' && this.BillType == 1) {
				Api.showTip("请选择供应商");
				return;
			}

			let index = e.currentTarget.detail.changeIndex;
			this.AddListens();
			let type = this.isChange ? "1" : "0";
			//保存修改的商品信息
			uni.setStorageSync("ChangeGoodData", JSON.stringify(this.OrderGoodsList[index]));
			uni.navigateTo({
				url: "../goodList/selectGoodInfo?selectIndex=" +
					index + "&IsCx=" + this.BillType + "&type=" + type + "&BillName=" + this.BillName + "&ClientID=" + this.SelectClientObj
					.ClientID
			});
		},
		//获取业务员列表
		GetOperatorMessage(Filter = "") {
			let data = {
				Filter: Filter
			}
			this.picker1 = [];
			var that = this;
			Api.GetOperatorMessage(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == "获取登录信息失败") {
						Api.showTip("登录超时");
					} else if (res.data.ErrorMsg != "") {
						Api.showTip(res.data.ErrorMsg);
					} else {
						let responseData = res.data.ResultMsg;
						if (responseData.length == 0) {
							Api.showTip("查无数据");
							return;
						}
						if (Filter != "") {
							that.TempOptionList = responseData;
						} else {
							that.OperatorList = responseData;
							that.TempOptionList = responseData;
						}
					}
				} else {

				}
			});
		},
		//增加商品事件监听
		AddListen() {
			var _this = this;
			//当前选择的商品的临时列表
			// var NewGoodListTemp = this.OrderGoodsList;
			this.$eventHub.$on('fires', function(data) {
				let GoodsItem = JSON.parse(data);
				uni.showLoading({
					title: "加载中..",
					mask: false
				})
				// console.log(_this.OrderGoodsList);
				_this.pullGoodsInfo(GoodsItem);
				_this.$eventHub.$off('fires');
			});
		},
		//判断加入的商品是否存在
		isInOrderList(GoodsItem) {
			let state = this.cgSettingLocation.onlyAddNum[0].state;
			let NewGoodListTemp = [];
			this.OrderGoodsList.forEach((item, index) => {
				NewGoodListTemp.push(item);
			});
			// GoodsItem = tempList;
			var unAddGood = [];
			if (this.BillType == 13) {
				GoodsItem.forEach((item, index) => {
					// if(this.IsInOrderList(item, state).isUnAdd){
					// 	 unAddGood.push(index);
					// }else{
					if (!this.IsCxInOrderList(item, state).isIn) {
						NewGoodListTemp.push(item);
					} else {
						Api.showTip("商品不能重复");
					}
					// }
				});
			} else {
				GoodsItem.forEach((item, index) => {
					// if(this.IsInOrderList(item, state).isUnAdd){
					// 	 unAddGood.push(index);
					// }else{
					if (!this.IsInOrderList(item, state).isIn) {
						NewGoodListTemp.push(item);
					}
					// }
				});
			}

			// if(unAddGood.length>0){
			// 	Api.showTip("批号商品批号不能为空");
			// }
			// return;
			this.OrderGoodsList = NewGoodListTemp;
			this.isSearch = false;
			this.searchKey = "";
		},
		//获取价格方案
		pullGoodsInfo(GoodsItem) {
			console.log(GoodsItem);
			this.AddGoodsTempList = GoodsItem;
			console.log(this.AddGoodsTempList);
			var that = this;
			for (var i in this.AddGoodsTempList) {
				this.XH_Get_PriceFollowMessage(this.AddGoodsTempList[i], i, function(res, index) {
					if (!that.AddGoodsTempList[index].isSet) {
						console.log(res, that.AddGoodsTempList[index]);
						that.AddGoodsTempList[index].Price = res;
						that.AddGoodsTempList[index].DiscountPrice = that.DiscountPrice(that.AddGoodsTempList[index]);
						that.AddGoodsTempList[index].Amount = that.Amount(that.AddGoodsTempList[index]);
					}

				});
			}
		},
		//判断当前增加的商品是否存在
		IsInOrderList(item, state) {
			// console.log("------");
			// console.log(item);

			for (var i = 0; i < this.OrderGoodsList.length; i++) {
				// console.log(this.OrderGoodsList[i]);
				// console.log(this.OrderGoodsList[i]);

				if (state) {
					if (this.OrderGoodsList[i].GoodsID == item.GoodsID && this.OrderGoodsList[i].Qty > 0) {
						item.isIn = true;
						this.OrderGoodsList[i].Qty = parseFloat(this.OrderGoodsList[i].Qty) + parseFloat(item.Qty);
						// this.OrderGoodsList[i].Remark = item.Remark;
						item.Price = this.OrderGoodsList[i].Price;
						item.Discount = this.OrderGoodsList[i].Discount;
						item.Tax = this.OrderGoodsList[i].Tax;
						this.OrderGoodsList[i].Amount = this.OrderGoodsList[i].Amount + parseFloat(item.Amount);
						// this.OrderGoodsList[i].DiscountPrice = parseFloat(item.DiscountPrice);
						return item;
					}
				} else {
					if (this.OrderGoodsList[i].GoodsID == item.GoodsID && this.OrderGoodsList[i].UnitID == item.UnitID &&
						parseFloat(this.OrderGoodsList[i].urate) == parseFloat(item.urate) &&
						parseFloat(this.OrderGoodsList[i].Price) == parseFloat(item.Price) && this.OrderGoodsList[i].StoreID ==
						item.StoreID &&
						parseFloat(this.OrderGoodsList[i].Discount) == parseFloat(item.Discount) &&
						parseFloat(this.OrderGoodsList[i].Tax) == parseFloat(item.Tax) && this.OrderGoodsList[i].IsGift == item.IsGift &&
						this.OrderGoodsList[i].Qty > 0
					) {
						item.isIn = true;
						this.OrderGoodsList[i].Qty = parseFloat(this.OrderGoodsList[i].Qty) + parseFloat(item.Qty);
						this.OrderGoodsList[i].Remark = item.Remark;
						this.OrderGoodsList[i].Amount = this.OrderGoodsList[i].Amount + parseFloat(item.Amount);
						// this.OrderGoodsList[i].DiscountPrice = parseFloat(item.DiscountPrice);
						return item;
					}
				}

			}
			item.isIn = false;
			return item;
		},
		//促销单的新增
		IsCxInOrderList(item, state) {
			// console.log("------");
			// console.log(item);

			for (var i = 0; i < this.OrderGoodsList.length; i++) {
				// console.log(this.OrderGoodsList[i]);
				// console.log(this.OrderGoodsList[i]);

				if (state) {
					if (this.OrderGoodsList[i].GoodsID == item.GoodsID && this.OrderGoodsList[i].Qty > 0) {
						item.isIn = true;
						// this.OrderGoodsList[i].Qty = parseFloat(this.OrderGoodsList[i].Qty) + parseFloat(item.Qty);
						// // this.OrderGoodsList[i].Remark = item.Remark;
						// item.Price = this.OrderGoodsList[i].Price;
						// item.Discount = this.OrderGoodsList[i].Discount;
						// item.Tax = this.OrderGoodsList[i].Tax;
						// this.OrderGoodsList[i].Amount = this.OrderGoodsList[i].Amount + parseFloat(item.Amount);
						// this.OrderGoodsList[i].DiscountPrice = parseFloat(item.DiscountPrice);
						return item;
					}
				} else {
					if (this.OrderGoodsList[i].GoodsID == item.GoodsID && this.OrderGoodsList[i].UnitID == item.UnitID &&
						this.OrderGoodsList[i].Qty > 0
					) {
						item.isIn = true;
						// this.OrderGoodsList[i].Qty = parseFloat(this.OrderGoodsList[i].Qty) + parseFloat(item.Qty);
						// this.OrderGoodsList[i].Remark = item.Remark;
						// this.OrderGoodsList[i].Amount = this.OrderGoodsList[i].Amount + parseFloat(item.Amount);
						// this.OrderGoodsList[i].DiscountPrice = parseFloat(item.DiscountPrice);
						return item;
					}
				}

			}
			item.isIn = false;
			return item;
		},
		getOnlyNumAmount(item) {
			return parseFloat(item.Price) * parseFloat(item.Qty) * (parseFloat(item.Discount) / 100) * (1 + parseFloat(item.Tax) /
				100); // return  PFPrice;
		},
		//商品修改事件监听
		AddListens() {
			var _this = this;
			this.$eventHub.$on('fireData', function(data) {
				// let NewGoodInfo = JSON.parse(data.NewGoodInfo);
				// _this.OrderGoodsList[data.index] = NewGoodInfo;
				// _this.OrderGoodsList[data.index].Tax = parseFloat(NewGoodInfo.Tax);
				// _this.OrderGoodsList[data.index].StoreID = NewGoodInfo.StoreID;
				// _this.OrderGoodsList[data.index].Qty = parseFloat(NewGoodInfo.Qty);
				// _this.OrderGoodsList[data.index].Discount = parseFloat(NewGoodInfo.Discount);
				// _this.OrderGoodsList[data.index].UnitName = NewGoodInfo.UnitName;
				// _this.OrderGoodsList[data.index].urate = NewGoodInfo.urate;
				// _this.OrderGoodsList[data.index].UnitID = NewGoodInfo.UnitID;
				// _this.OrderGoodsList[data.index].Price = NewGoodInfo.Price;
				// _this.OrderGoodsList[data.index].DiscountPrice = NewGoodInfo.DiscountPrice;
				// _this.OrderGoodsList[data.index].Amount = NewGoodInfo.Amount;
				// _this.OrderGoodsList[data.index].stockqty = NewGoodInfo.stockqty;	
				// this.GoodList[index].isSet = true;
				// console.log(GoodList);
				let NewGoodInfo = JSON.parse(data.NewGoodInfo);
				_this.OrderGoodsList[data.index] = NewGoodInfo;
				let OrderGoodsList = Object.assign([], _this.OrderGoodsList);


				_this.pullBlissfulGoods(data.index, OrderGoodsList);
				_this.searchKey = "";

				// console.log(_this.OrderGoodsList[data.index]);
				_this.selectLookMore = -1;
				_this.$eventHub.$off('fireData');
			});
		},
		//相同的参数商品合并
		pullBlissfulGoods(index, OrderGoodsList) {
			let item = OrderGoodsList[index];
			OrderGoodsList.forEach(($item, $index) => {
				if ($index != index) {
					if ($item.GoodsID == item.GoodsID && $item.UnitID == item.UnitID &&
						parseFloat($item.urate) == parseFloat(item.urate) &&
						parseFloat($item.Price) == parseFloat(item.Price) && $item.StoreID ==
						item.StoreID &&
						parseFloat($item.Discount) == parseFloat(item.Discount) &&
						parseFloat($item.Tax) == parseFloat(item.Tax) && $item.IsGift == item.IsGift && $item.Qty > 0
					) {
						item.Qty = parseFloat(item.Qty) + parseFloat($item.Qty);
						item.Remark = $item.Remark;
						item.Amount = parseFloat(item.Amount) + parseFloat($item.Amount);
						$item.Remark = "";
						$item.Amount = "";
						$item.Qty = 0;
					}
				}
			});
			this.OrderGoodsList = OrderGoodsList;
		},
		//获取仓库
		GetStoreMessage(Filter = "") {
			let data = {
				Filter: Filter
			}
			var that = this;
			Api.GetStoreMessage(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == "获取登录信息失败") {
						Api.showTip("登录超时");
					} else if (res.data.ErrorMsg != "") {
						Api.showTip(res.data.ErrorMsg);
					} else {
						// if(typeof res.data.ResultMsg.length == 'object'){
						// 	if()
						// 	Api.showTip("查无数据");
						// 	return;
						// }
						if (Filter != "") {
							that.TempStoreList = res.data.ResultMsg;
						} else {
							that.StoreList = res.data.ResultMsg;
							that.TempStoreList = res.data.ResultMsg;
						}
						if (that.TempStoreList == null || that.TempStoreList == "") {
							Api.showTip("查无数据");
						}
					}
				} else {

				}
			});
		},
		//点击获取单号
		GetNewOrderNo(pull) {
			this.getBillTime();
			if (!pull) {
				this.newMoreInput();
			} else {
				if (this.BillNo == "") {
					Api.showTip("请先创建新单");
					return;
				}
				//如果是促销单
				if (this.BillType == 13) {
					if (this.startTime == "") {
						Api.showTip("请选择特价开始时间");
						return;
					}
					if (this.endTime == "") {
						Api.showTip("请选择特价结束时间");
						return;
					}
				}
				if (this.BillDate == "") {
					Api.showTip("开单时间不能为空");
					return;
				}
				if (this.SelectClientObj.ClientID == "") {
					Api.showTip(this.BillType == 1 ? "请选择供应商" : "请选择客户");
					return;
				}
				if (this.SelectStoreObj.StoreID == "") {
					Api.showTip("请选择仓库");
					return;
				}
				if (this.OrderGoodsList.length == 0) {
					Api.showTip("请至少选择一件商品");
					return;
				}
			}
			if (this.isUserRoot.IsBill_AddNew == "") {
				Api.showTip("没有新增权限");
				return;
			}
			let data = {
				BillType: this.BillType,
				IsAdd: pull ? 1 : 0,
				BillDate: this.BillDate
			};
			var that = this;
			Api.NewOrderNo(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == "获取登录信息失败") {
						Api.showTip("登录超时");
					} else if (res.data.ErrorMsg != "") {
						Api.showTip(res.data.ErrorMsg);
					} else {
						that.BillNo = res.data.ResultMsg.BillNo
						if (pull) {
							that.AddBillOrder();
						}
					}
				} else {

				}
			});
		},
		tabSelect(e) {
			this.TabCur = e.currentTarget.dataset.id;
			console.log(22)
			this.scrollLeft = (e.currentTarget.dataset.id - 1) * 60
		},

		xiala(e) {
			var i = e.currentTarget.dataset.findex;
			console.log(i)

		},
		// 单据日期
		open() {
			this.$refs.calendar.open();
		},
		// 特价开始日期
		openStart() {
			this.$refs.calendar2.open();
		},
		// 特价结束日期
		openEnd() {
			this.$refs.calendar3.open();
		},
		// 单据日期选择事件
		confirm(e) {
			console.log(e)
			this.timeData = e
		},
		// 特价开始日期选择事件
		confirm1(e) {
			console.log(e)
			this.startTime = e.fulldate
		},
		// 特价结束日期选择事件
		confirm2(e) {
			console.log(e)
			this.endTime = e.fulldate
		},
		gogoodlist() {
			if (this.BillNo == "") {
				Api.showTip("请先创建新单");
				return;
			};
			console.log(this.SelectStoreObj.StoreID);
			if (this.SelectStoreObj.StoreID == "") {
				Api.showTip("请先选择仓库");
				return;
			};
			if (this.SelectClientObj.ClientID == "" && this.BillType == 1) {
				Api.showTip("请先选择供应商");
				return;
			}
			if (this.SelectClientObj.ClientID == "" && this.BillType == 4) {
				Api.showTip("请先选择客户");
				return;
			}
			this.AddListen();
			this.isGetPriceFollowMessage = [];
			var isSelectMore = this.cgSettingLocation.isMoreSelectGood[0].state ? "1" : "0";
			uni.navigateTo({
				url: "../goodList/goodList?StoreID=" + this.SelectStoreObj.StoreID + "&IsCx=" + this.BillType +
					"&BillName=" + this.BillName + "&isSelectMore=" + isSelectMore + "&ClientID=" + this.SelectClientObj.ClientID
			});
		},
		//显示全部分类
		showType: function() {
			if (this.isShowGoodsType == 0) {
				this.isShowGoodsType = 1;
			} else {
				this.isShowGoodsType = 0;
			}

		},
		//添加商品
		// addSelectGoods() {
		// 	if (this.NowSelectGoods.GoodsName == undefined) {
		// 		Api.showTip("请先选择商品");
		// 		return;
		// 	}
		// 	if (this.GoodsQty == "" || this.GoodsQty < 1) {
		// 		Api.showTip("商品数量不能少于1件");
		// 		return;
		// 	}
		// 	if (this.GoodsDiscount == "") {
		// 		Api.showTip("请输入折扣价");
		// 		return;
		// 	}
		// 	if (this.index3 == -1) {
		// 		Api.showTip("请选择商品仓库");
		// 		return;
		// 	}
		// 	//判断商品是否存在 0否 1 是
		// 	var flag = 0;
		// 	var _this = this;
		// 	// console.log(this.NowSelectGoods);
		// 	console.log(this.GoodsQty);
		// 	console.log(this.GoodsRemark);
		// 	console.log(this.GoodsDiscount);
		// 	this.NowSelectGoods.Qty = this.GoodsQty;
		// 	this.NowSelectGoods.Remark = this.GoodsRemark;
		// 	this.NowSelectGoods.Discount = this.GoodsDiscount;
		// 	this.NowSelectGoods.GoodsStorm = this.GoodsStorm;
		// 	this.NowSelectGoods.Tax = this.Tax;
		// 	this.NowSelectGoods.StoreID = this.StoreList[this.index3].StoreID;
		// 	console.log(this.NowSelectGoods);
		// 	this.OrderGoodsList.forEach((item, index) => {
		// 		if (item.GoodsID == _this.NowSelectGoods.GoodsID) {
		// 			_this.OrderGoodsList[index].Remark = _this.GoodsRemark;
		// 			_this.OrderGoodsList[index].Qty = _this.GoodsQty;
		// 			_this.OrderGoodsList[index].Discount = _this.GoodsDiscount;
		// 			_this.OrderGoodsList[index].GoodsStorm = _this.GoodsStorm;
		// 			_this.OrderGoodsList[index].Tax = _this.Tax;
		// 			_this.OrderGoodsList[index].StoreID = _this.StoreList[this.index3].StoreID;
		// 			flag = 1;
		// 		}
		// 	});
		// 	console.log(flag);
		// 	if (flag == 0) {
		// 		this.OrderGoodsList.push(this.NowSelectGoods);
		// 	}
		// 	this.GoodsQty = "";
		// 	this.GoodsRemark = "";
		// 	this.GoodsDiscount = "";
		// 	this.GoodsStorm = "";
		// 	this.Tax = "";
		// 	console.log(this.OrderGoodsList);
		// 	this.NowSelectGoods = {};
		// 	this.isSelectGood = false;
		// },
		showModal(e) {
			console.log(1111)
			this.modalName = e.currentTarget.dataset.target
		},

		choosesunit(e) {
			console.log(e)
			var unit = e.currentTarget.dataset.unit
			this.unitselected = unit
			this.modalName = ""
		},
		//计算折扣后的总金额
		compuTaxMoney(item) {
			let PFPrice = "";
			if (this.isChange) {
				PFPrice = parseFloat(item.Price) * parseFloat(item.Qty) * (parseFloat(item.Discount) / 100) * (1 + parseFloat(
					item.Tax) / 100);
			} else {
				PFPrice = parseFloat(item.Price) * parseFloat(item.Qty) * (parseFloat(item.Discount) / 100) * (1 + parseFloat(
					item.Tax) / 100);
			}
			return PFPrice;
		},
		isPrintOrder(type, BillID = "") {
			var that = this;
			if (type) {
				Api.showConfirm("是否打印单据", function(result) {
					if (result) {
						that.PrintOrder(false, BillID);
					} else {
						setTimeout(function() {
							if (!that.isBillCheckQx) {
								if (that.cgSettingLocation.djSh[0].state) {
									that.isBillCheck(true);
								}
								if (that.cgSettingLocation.djSh[1].state) {
									that.isBillCheck(false);
								}
							} else {
								that.isBillCheck(false);
							}
						}, 1000);
					}
				})
			} else {
				that.PrintOrder(false, BillID);
			}
		},
		PrintOrder(isInput = true, BillID = "") {
			let data = {};
			let Save_M_Data_Json = {};
			if (!isInput) {
				Save_M_Data_Json = {
					Order_Number: this.BillNo,
					BillName: this.BillName,
					Printer: this.CreateName,
					BillID: this.BillID
				};
			} else {
				this.modalNames = null;
				Save_M_Data_Json = {
					Order_Number: this.BillNo,
					BillName: this.BillName,
					Printer: this.CreateName,
					BillID: this.BillID,
					PrintNum: this.printNum + "",
					PrintPCNumber: this.printCode + ""
				};
			}
			data = {
				Save_M_Data_Json: JSON.stringify(Save_M_Data_Json)
			}
			this.printNum = 1;
			this.current = 0;
			this.printCode = 1;
			var _this = this;
			Api.Add_Android_Print(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == '获取登录信息失败') {
						Api.showTip("登录超时");
					} else if (res.data.ErrorMsg != "") {
						Api.showTip(res.data.ErrorMsg);
					} else {
						Api.showTip("已发送打印指令");
						//如果不是按钮触发事件
						if (!isInput) {
							setTimeout(function() {
								if (!_this.isBillCheckQx) {
									if (_this.cgSettingLocation.djSh[0].state) {
										_this.isBillCheck(true);
									}
									if (_this.cgSettingLocation.djSh[1].state) {
										_this.isBillCheck(false);
									}
								} else {
									_this.isBillCheck(false);
								}
							}, 1000);
						}
					}
				}
			})
		},
		isBillCheck(type) {
			var that = this;
			if (type) {
				Api.showConfirm("是否审核单据", function(result) {
					if (result) {
						that.toBillCheck();
					} else {

					}
				})
			} else {
				that.toBillCheck();
			}
		},
		toBillCheck(isInput, BillID = "") {
			var that = this;
			let data = {
				BillType: this.BillType,
				BillID: this.BillID,
				BillName: this.BillName
			};
			Api.Bill_Check(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == '获取登录信息失败') {
						Api.showTip("登录超时!");
					} else if (res.data.ErrorMsg != "") {
						Api.showTip(res.data.ErrorMsg);
					} else {
						// that.isBillCheckSuc[that.isBillCheck] = checkCountList[that.isBillCheck];
						// that.isBillCheck++;
						Api.showTip("审核成功");
					}
				} else {

				}
				// that.toBillCheck(checkCountList, index, countSize);
			})
		},
		//提交单据
		AddBillOrder() {
			this.searchKey = "";
			var _this = this;
			// if (this.index1 == -1 && this.BillType == 4 && this.BillType == 13) {
			// 	this.index1 = 0;
			// }
			var _this = this;
			let CreateID = uni.getStorageSync("UserCode");
			let CreateName = uni.getStorageSync("UserName");
			let Total = 0;
			this.OrderGoodsList.forEach((item, index) => {
				Total += this.compuTaxMoney(item);
			});
			let Save_M_Data_Json = {
				BillNo: _this.BillNo,
				BillTime: _this.BillDate,
				ClientID: _this.SelectClientObj.ClientID,
				StoreID: _this.SelectStoreObj.StoreID,
				EmpID: _this.SelectOptionsObj.OperatorID,
				Amount: Total + "",
				CreateID: CreateID,
				CreateName: this.CreateName,
				Comment: _this.Remark,
				startTime: this.startTime,
				EndTime: this.endTime
			};
			var Save_D_Data_Json = [];
			this.OrderGoodsList.forEach((item, index) => {
				let itemData = {
					BillType: this.BillType + "",
					GoodsID: item.GoodsID,
					UnitID: item.UnitID,
					URate: item.urate,
					Discount: item.Discount + "",
					Tax: item.Tax + "",
					Qty: item.Qty + "",
					Price: item.Price + "",
					Amount: this.compuTaxMoney(item) + "",
					Remark: item.Remark,
					String3: item.barcode,
					StoreID: item.StoreID
				};
				Save_D_Data_Json.push(itemData);
			});
			let data = {
				BillType: this.BillType,
				Save_M_Data_Json: JSON.stringify(Save_M_Data_Json),
				Save_D_Data_Json: JSON.stringify(Save_D_Data_Json)
			};
			Api.AddBillOrder(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == "获取登录信息失败") {
						Api.showTip("登录超时");
					} else if (res.data.ErrorMsg != "") {
						Api.showTip(res.data.ErrorMsg);
					} else {
						_this.OrderGoodsList = [];
						Api.showTip("保存成功");
						_this.BillID = res.data.BillID;
						if (_this.cgSettingLocation.ycDy[0].state) {
							_this.isPrintOrder(true, res.data.BillID);
						}
						if (_this.cgSettingLocation.ycDy[1].state) {
							_this.isPrintOrder(false, res.data.BillID);
						}
						//如果没有勾选打印
						if (!_this.cgSettingLocation.ycDy[0].state && !_this.cgSettingLocation
							.ycDy[1].state) {
							if (!_this.isBillCheckQx) {
								if (_this.cgSettingLocation.djSh[0].state) {
									_this.isBillCheck(true);
								}
								if (_this.cgSettingLocation.djSh[1].state) {
									_this.isBillCheck(false);
								}
							} else {
								_this.isBillCheck(false);
							}

						}
						_this.sureClear(true);
					}
				} else {

				}
			});


		}
	}
}
